﻿(function () {
    $(function () {
        var _postService = abp.services.app.post;
        var _$modal = $("#PostCreateModal");
        var _$form = _$modal.find("form");
        //添加部门
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }
            var post = _$form.serializeFormToObject();

            abp.ui.setBusy(_$modal);
           
            _postService.createOrUpdatePost({ post }).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost()
        });

        function refreshpost() {
            location.reload();
        }

        //删除

        $(".delete-post").click(function () {


            var postId = $(this).attr("data-post-id");
            var postName = $(this).attr("data-post-name");
            // alert(orgId + orgName);
            deletePost(postId, postName);
        });

            function deletePost(postId, postName) {

            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DualControl'), postName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _postService.deletePost({
                            id: postId
                        }).done(function () {
                            refreshpost();
                        });
                    }
                }
            );
        }

        //编辑岗位
        $(".edit-post").click(function (e) {
            e.preventDefault();
            var postId = $(this).attr("data-post-id");          
            //查询赋值
            _postService.getPostForEdit({ id: postId }).done(function (data) {               
               
                $("input[name=id]").val(data.post.id);
                $("input[name=Code").val(data.post.code).parent().addClass("focused");
                $("input[name=Name").val(data.post.name).parent().addClass("focused");
                $("input[name=Responsibility").val(data.post.responsibility).parent().addClass("focused");
            });
        });

        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();